package pack;

import java.io.Serializable;
import java.util.List;

import javax.ejb.EJB;
import javax.faces.application.ConfigurableNavigationHandler;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import dao.RaspunsuriDAORemote;
import dto.EmployeeDTO;
import dto.IntrebariDTO;
import dto.RaspunsuriDTO;

@ManagedBean(name = "raspunsBean")
@SessionScoped
public class RaspunsBean implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@EJB
	RaspunsuriDAORemote raspunsDAO;

	@ManagedProperty(value = "#{raspunsuriDTO}")
	private RaspunsuriDTO raspunsuriDTO;

	private int id;

	private int id_intrebare;

	private String raspuns;

	List<RaspunsuriDTO> raspunsuri;

	RaspunsuriDTO raspunsUpd;

	public void verifyA() {
		FacesContext fc = FacesContext.getCurrentInstance();
		EmployeeDTO eDTO = (EmployeeDTO) fc.getExternalContext()
				.getSessionMap().get("loggedInUser");

		ConfigurableNavigationHandler nav = (ConfigurableNavigationHandler) fc
				.getApplication().getNavigationHandler();

		if (eDTO != null) {
			if (eDTO.getRank() != 1) {
				nav.performNavigation("home.xhtml");
			}
		} else {
			nav.performNavigation("home.xhtml");
		}
		read();
	}

	public void resetFields() {
		this.setRaspuns("");
	}

	public String read() {
		FacesContext fc = FacesContext.getCurrentInstance();
		IntrebariDTO intreb = new IntrebariDTO();
		intreb = (IntrebariDTO) fc.getExternalContext().getSessionMap()
				.get("intrebarea");
		int id = intreb.getId();
		raspunsuri = raspunsDAO.getRaspunsuriByIntrebareId(id);
		this.setRaspunsuri(raspunsuri);
		return "";
	}

	public String create() {
		FacesContext fc = FacesContext.getCurrentInstance();
		IntrebariDTO intreb = new IntrebariDTO();
		intreb = (IntrebariDTO) fc.getExternalContext().getSessionMap()
				.get("intrebarea");
		RaspunsuriDTO raspunsuri = new RaspunsuriDTO();
		raspunsuri.setIntrebari(intreb);
		//raspunsuri.setId_intrebare(intreb_id);
		raspunsuri.setRaspuns(getRaspuns());
		raspunsDAO.create(raspunsuri);
		resetFields();
		return "";
	}

	public String delete(RaspunsuriDTO raspuns) {
		raspunsDAO.delete(raspuns);
		return "intrebariUpdate";
	}

	public String delRas(IntrebariDTO intrebare) {
		if (intrebare.getTip() != 3) {
			FacesContext fc = FacesContext.getCurrentInstance();
			fc.getExternalContext().getSessionMap()
					.put("intrebarea", intrebare);
			setRaspunsuri(raspunsDAO.getRaspunsuriByIntrebareId(intrebare
					.getId()));
			return "raspunsuriDelete.xhtml";
		}
		return "";
	}

	public String actRas(IntrebariDTO intrebare) {
		if (intrebare.getTip() != 3) {
			FacesContext fc = FacesContext.getCurrentInstance();
			fc.getExternalContext().getSessionMap()
					.put("intrebarea", intrebare);
			setRaspunsuri(raspunsDAO.getRaspunsuriByIntrebareId(intrebare
					.getId()));
			return "raspunsuriRead.xhtml";
		}
		return "";
	}

	public String edit(RaspunsuriDTO raspuns) {
		setRaspunsUpd(raspuns);
		return "raspunsuriUpdate.xhtml";
	}

	public String update() {
		raspunsDAO.update(raspunsUpd);
		return "";
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getId_intrebare() {
		return id_intrebare;
	}

	public void setId_intrebare(int id_intrebare) {
		this.id_intrebare = id_intrebare;
	}

	public String getRaspuns() {
		return raspuns;
	}

	public void setRaspuns(String raspuns) {
		this.raspuns = raspuns;
	}

	public RaspunsuriDTO getRaspunsuriDTO() {
		return raspunsuriDTO;
	}

	public void setRaspunsuriDTO(RaspunsuriDTO raspunsuriDTO) {
		this.raspunsuriDTO = raspunsuriDTO;
	}

	public List<RaspunsuriDTO> getRaspunsuri() {
		return raspunsuri;
	}

	public void setRaspunsuri(List<RaspunsuriDTO> raspunsuri) {
		this.raspunsuri = raspunsuri;
	}

	public RaspunsuriDTO getRaspunsUpd() {
		return raspunsUpd;
	}

	public void setRaspunsUpd(RaspunsuriDTO raspunsUpd) {
		this.raspunsUpd = raspunsUpd;
	}


}
